﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using System.Text;

namespace Actuals.Data
{
    public class DalTask : Dal<ITask, Task>, IDalTask
    {


        private readonly IDalManager _manager;
        private static readonly IDalTask _instance = new DalTask();
        private const string SP_GET_TASKS = "usp_GetTasks";
        private const string SP_GET_DASHBOARD_TASKS = "usp_GetDashBoardTasks";
        private const string SP_GET_TASK = "usp_GetTask";
        private const string SP_ADD_AND_UPDATE_TASK = "usp_AddAndUpdateTask";
        private const string SP_DELETE_TASK = "usp_DeleteTask";


        public static IDalTask Instance
        {
            get { return _instance; }
        }

        public DalTask()
        {
            _manager = new DalManager();
        }

        public DalTask(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(ITask obj, DataRow row)
        {
            obj.ProjectId = _manager.GetValue<int>(row, "ProjectId");
            obj.ReleaseId = _manager.GetValue<int>(row, "ReleaseId");
            obj.TaskId = _manager.GetValue<int>(row, "TaskId");
            obj.TaskType = _manager.GetValue<string>(row, "TaskType");
            obj.TaskShortName = _manager.GetValue<string>(row, "TaskShortName");
            obj.Description = _manager.GetValue<string>(row, "Description");
            obj.InitialEstimation = _manager.GetValue<int>(row, "InitialEstimation");
            obj.CurrentEstimation = _manager.GetValue<int>(row, "CurrentEstimation");
            obj.Elapsed = _manager.GetValue<int>(row, "Elapsed");
            obj.Remaining = _manager.GetValue<int>(row, "Remaining");
            obj.Status = _manager.GetValue<int>(row, "Status");
            obj.Priority = _manager.GetValue<int>(row, "Priority");



        }
        public IList<ITask> GetTasks(int projectId, int releaseId)
        {
            _manager.StoredProcedureName = SP_GET_TASKS;
            _manager.AddInParameter("@ProjectId", DbType.Int32, projectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, releaseId);

            List<ITask> tasks = new List<ITask>();
            foreach (ITask task in ConvertAll(_manager.ExecuteDataSet()))
                tasks.Add(task);

            return tasks;
        }

        public IList<ITask> GetDashBoardTasks(int projectId, int releaseId)
        {
            _manager.StoredProcedureName = SP_GET_DASHBOARD_TASKS;
            _manager.AddInParameter("@ProjectId", DbType.Int32, projectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, releaseId);

            List<ITask> tasks = new List<ITask>();
            foreach (ITask task in ConvertAll(_manager.ExecuteDataSet()))
                tasks.Add(task);

            return tasks;
        }

        public ITask GetTask(int taskId)
        {
            _manager.StoredProcedureName = SP_GET_TASK;
            _manager.AddInParameter("@TaskId", DbType.Int32, taskId);
            ITask task = new Task();
            Convert(task, _manager.ExecuteDataSet());
            return task;
        }
        public int AddTask(ITask task)
        {
            _manager.StoredProcedureName = SP_ADD_AND_UPDATE_TASK;
            _manager.AddInParameter("@ProjectId", DbType.Int32,task.ProjectId );
            _manager.AddInParameter("@ReleaseId", DbType.Int32, task.ReleaseId );
            _manager.AddInParameter("@TaskId", DbType.Int32, task.TaskId );
            _manager.AddInParameter("@TaskType", DbType.String , task.TaskType );
            _manager.AddInParameter("@TaskShortName", DbType.String , task.TaskShortName);
            _manager.AddInParameter("@Description", DbType.String , task.Description);
            _manager.AddInParameter("@Status", DbType.Int32  , task.Status);
            _manager.AddInParameter("@InitialEstimation", DbType.Int32, task.InitialEstimation);
            _manager.AddInParameter("@CurrentEstimation", DbType.Int32, task.CurrentEstimation);
            _manager.AddInParameter("@Priority", DbType.Int32, task.Priority);
            


            return _manager.ExecuteNonQuery();
        }

        public int UpdateTask(ITask task)
        {
            _manager.StoredProcedureName = SP_ADD_AND_UPDATE_TASK;
            _manager.AddInParameter("@ProjectId", DbType.Int32,task.ProjectId);
            _manager.AddInParameter("@ReleaseId", DbType.Int32, task.ReleaseId);
            _manager.AddInParameter("@TaskId", DbType.Int32, task.TaskId);
            _manager.AddInParameter("@TaskType", DbType.String, task.TaskType);
            _manager.AddInParameter("@TaskShortName", DbType.String, task.TaskShortName);
            _manager.AddInParameter("@Description", DbType.String, task.Description);
            _manager.AddInParameter("@Status", DbType.Int32, task.Status);
            _manager.AddInParameter("@InitialEstimation", DbType.Int32, task.InitialEstimation);
            _manager.AddInParameter("@CurrentEstimation", DbType.Int32, task.CurrentEstimation);
            _manager.AddInParameter("@Priority", DbType.Int32, task.Priority);

            return _manager.ExecuteNonQuery();
            
        }

       
        public bool DeleteTask(int taskId)
        {

            _manager.StoredProcedureName = SP_DELETE_TASK;
            _manager.AddInParameter("@TaskId", DbType.Int32, taskId);
            _manager.ExecuteNonQuery();
            return true;
        }
        

        }

    }










